<h1> Hello PHP </h1>
<?php
	// PHP : Hypertext Preprocessor
	echo "<p>I'm getting good with PHP.</p>";
	// 变量名用 dollar 号作为前缀
	$message = "Hello world";
	echo $message;

	// 字符串类型的常量可以用单引号和双引号
	$msg = "'Example'<br/>";
	$another_msg = '"Example2."<br/>';
	echo $msg;
	echo $another_msg;
	$num = 4;
	// 双引号字符串可以进行 interpolation
	echo "Helei says : \"$num\".<br/>";
	echo "This is ${msg}<br/>";
	echo "This is $ {msg}<br/>";
	// 新建一个 array
	$arr = array(1, 2, "4");
	for ($i=0; $i < 3; $i++) { 
		echo $arr[$i], "<br/>";
	}
	// 新建一个关联 array
	$map = array(
			"helei" => 1,
			"leihe" => 2,
			"hehe" => 3,
			"leilei" => array("hahaha")
		);
	echo $map["helei"], "<br/>";
	// 算术运算符
	$num = $num + 1;
	$num *= 5;
	$num = 5 / $num;
	$num *= "10000";
	echo $num, "<br/>";
	// 字符串连接运算符
	$str = "hello str".".hello str.<br/>";
	echo $str;
	// 比较运算符
	// 双等号尝试转换类型
	if (1 == "1") {
		echo "1 == \"1\"<br/>";
	}
	// 三等号直接比较, 不进行类型转换
	if (1 === "1") {
		echo "wtf.<br/>";
	}
	// ... 大于小于号
	// 逻辑运算符
	$sher = "sherlock";
	$holmes = false;
	$wat = array();
	if (($sher && $holmes) || !$wat) {
		echo "I'm sherlocked.<br/>";
	}
	// 条件运算符
	$sher = 5 > 3 ? "s" : "t";
	echo "$sher <br/>";
	// 函数的定义和使用
	function recursive_hello($time) {
		if ($time === 1) {
			echo "hello<br/>";
			return;
		}
		echo "hello.";
		recursive_hello($time - 1);
		return;
	}
	$a = recursive_hello(10);
	// if 控制结构
	$person = array("name" => "Watson");
	if ($person["name"] === "Watson") {
		echo "Hello Watson! <br/>";
	} elseif ($person["name"] === "John") {
		echo "Hello John~ <br/>";
	} else {
		echo "Who are you? <br/>";
	}
	// loop 顺序数组
	$arr = array(1, 1, 2, 3, 5, 8);
	for ($i=0, $len = count($arr); $i < $len; $i++) { 
		echo $arr[$i], ",";
	}
	echo "<br/>"; 
	// loop 关联数组
	// foreach 也可以 loop 顺序数组
	$arr = array(
			"name" => "He Lei",
			"hobby" => "eat",
			"age" => 23
		);
	echo "<ul>";
	foreach ($arr as $key => $value) {
		echo "<li>$key : $value</li>";
	}
	echo "</ul>";
	// switch 控制结构
	$person = "a";
	switch ($person) {
	case "a":
		echo "it's a<br/>";
		break;
	case "b":
		echo "it's b<br/>";
		break;
	default :
		echo "it's nothing<br/>";
	}
	// 文件包含指令
	include "friends.php";
	echo $best, "<br/>";
	include "friends.php";
	echo $best, "<br/>";

	include "friends.php";
	echo $best, "<br/>";
	include_once "friends.php";
	echo $best, "<br/>";

	// require "not_here.php";
	// include "not_here.php";

	// PHP 字符串函数
	// 输出
	$str = "str test k<br/>";
	print($str);
	echo "<pre>";
	print_r($arr);
	echo "</pre>";
	// 拆分组合
	$a = explode(" ", $str);
	echo "<pre>";
	print_r($a);
	echo "</pre>";
	$b = implode("-", $a);
	print($b);
	// 改变大小写
	print(lcfirst("FUCK YOU <br/>"));
	print(ucfirst("fuck you <br/>"));
	print(ucwords("fuck you <br/>"));
	print(strtolower("FucK You <br/>"));
	print(strtoupper("FucK You <br/>"));
	// 修剪多余字符
	echo trim("---asdasd---", "-"), "<br/>";
	// 子串替换
	echo str_replace("Tom", "Helei", "Tom is NB."), "<br/>";
	echo str_ireplace("tOM", "Helei", "Tom is NB."), "<br/>";
	// 字符串长度
	echo " 'aaa' has ".strlen("aaa")." chars <br/>";
	// 搜索子字符串
	echo strpos("tee too ta", "to"), "<br/>";

	// PHP 数组函数
	// push,pop & shift,unshift
	$arr = array(1, 2, 3, 4);
	array_push($arr, 5, 6, 7);
	echo array_pop($arr), "<br/>";
	array_unshift($arr, -2, -1, 0);
	echo array_shift($arr), "<br/>";
	// functional
	// walk, map, reduce
	function say_yeah($name) {
		echo "$name says : 'Yeah.' <br/>";
	}
	$persons = array("Tom", "Jerry", "Frank");
	array_walk($persons, "say_yeah");
	function change_name($name) {
		return $name." (bitch)";
	}
	print_r(array_map("change_name", $persons));
	echo "<br/>";
	function cat_name($name1, $name2) {
		return $name1.$name2;
	}
	echo array_reduce($persons, "cat_name"), "<br/>";
	// 判断是否含有某个元素
	$persons = array("Tom", "Jerry", "Frank");
	$index = array_search("Tom", $persons);
	echo "$index <br/>";
	if (in_array("Frank", $persons)) {
		echo "Frank is in array <br/>";
	}
	// 数组 slicing
	print_r(array_slice($persons, 1));
	echo "<br/>";
	array_splice($persons, 1, 2, array(1, 2));
	print_r($persons);
	echo "<br/>";
	// 数组排序 (in place)
	$arr = array(5, 2, 1, 19, 12.5);
	asort($arr);
	print_r($arr);
	echo "<br/>";
	// 数组元素总数, 数组元素和
	echo array_sum($arr), "<br/>";
	echo count($arr), "<br/>";

	// PHP 时间日期函数
	// 解析日期字符串
	print_r(date_parse("2012-05-6 13:46"));
	echo "<br/>";
	// 获取 timestamp
	echo time(), "<br/>";
	echo strftime("%B %d, %Y"), "<br/>";

	$arr = array();
	// PHP 数学函数
	for ($i=0; $i < 5; $i++) { 
		array_push($arr, round(mt_rand(0, 10) + 0.5));
	}
	print_r($arr);
	echo "<br/>";

	// JSON 编解码
	$person = array(
		"name" => "Sherlock Holmes",
		"job" => "detective");
	print_r(json_decode(json_encode($person), true));
	echo "<br/>";

	// 文件操作函数
	// 读取文件
	$quote = file_get_contents("./text.txt");
	echo $quote;
	$f = fopen("text.txt", "r");
	while (($quote = fgets($f)) !== false)
		echo $quote;
	echo "<br/>";
	rewind($f);
	echo fread($f, 3);
	echo "<br/>";
	fclose($f);
	// 写文件
	file_put_contents("output.txt", "lalalala.\n");
	$f = fopen("out2.txt", "w");
	fwrite($f, "what the fuck.");
	fclose($f);
	
?>

